package xiaoqi.springboot.boot.producer;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import xiaoqi.springboot.boot.message.Demo06Message;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

@Component
public class Demo06Producer {
    @Resource
    private KafkaTemplate<Object, Object> kafkaTemplate;

    public SendResult syncSendOrderly(Integer id) throws ExecutionException, InterruptedException {
        Demo06Message message = new Demo06Message();
        message.setId(id);
        //  同步发送消息
        // 使用率 String 的方式序列化 key, 所以要将 ID 转为 String
        return kafkaTemplate.send(Demo06Message.TOPIC, String.valueOf(id), message).get();
    }

}
